//
//  MessageAllNoteViewController.m
//  ZJControlsClass
//
//  Created by mac on 2024/11/24.
//

#import "MessageAllNoteViewController.h"
#import <Masonry/Masonry.h>
#import "ZJMessageNetworking.h"
#import "ZJMessageListModel.h"
#import "ZJMessageTableViewCell.h"
#import "publicNodataView.h"


@interface MessageAllNoteViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic,strong) ZJMessageNetworking * manager;
//@property (nonatomic,strong) NSMutableDictionary * paramer;
@property (nonatomic, assign) NSInteger pageNo;
@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic,strong) NSMutableArray * dataSource;

@property (nonatomic,strong) publicNodataView * nodataView;


@end

@implementation MessageAllNoteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor backgroundGrayColor];
    
    [self setupTableView];
    [self setupData];
    [self networking];

}

- (void)setupData {
    self.pageNo = 1;
    self.pageSize = 20;

}

- (void)setupTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.estimatedRowHeight = 100;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.tableView.contentInset = UIEdgeInsetsMake(0, 12, 0, 12);
  
    __weak typeof(self) weakSelf = self;
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pageNo = 1;

              [weakSelf networking];
          }];
          _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
              weakSelf.pageNo += 1;
             
              [weakSelf networking];
          }];
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.view);
        make.left.equalTo(self.view).offset(12);
        make.right.equalTo(self.view).offset(-12);
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
    [self.view addSubview:self.nodataView];
    [self.nodataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];
    
    
    
}

-(void)networking{
    
    
    WeakSelf(self)
//    __weak typeof(self) weakSelf = self;
    [self.manager dispatchTaskWithParam:[self parameters] completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (responseObject && [responseObject[@"code"] integerValue] == 200) {
                
            if (weakSelf.pageNo == 1) {
                [weakSelf.dataSource removeAllObjects];
            }
            NSArray * data = responseObject[@"data"][@"list"];
            if (data.count == 0) {
                self.nodataView.hidden = NO;
            }else{
                self.nodataView.hidden = YES;
            }
            
            
            if (data.count < 20)[self.tableView.mj_footer endRefreshingWithNoMoreData];
            else [weakSelf.tableView.mj_footer endRefreshing];
            [weakSelf.dataSource  addObjectsFromArray:[ZJMessageListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]]];
               
            [self.tableView reloadData];
            
        }else{
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:^{
                
            }];
        }
        
        [self.tableView.mj_header endRefreshing];
        
        
    }];
    
    
    
}



#pragma mark - UITableViewDataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    static NSString *cellID = @"ZJMessageTableViewCell";
    ZJMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[ZJMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    }
    cell.model = self.dataSource[indexPath.row];

//    cell.sureBtn addGestureRecognizer:<#(nonnull UIGestureRecognizer *)#>
//    HomeWebViewViewController * vc = [HomeWebViewViewController new];
//    vc.Id = model.id;
//    [[SaViewControllerUtils currentViewController].navigationController pushViewController:vc animated:YES];
// [_sureBtn addTarget:self action:@selector(respondsTo_checkCancel) forControlEvents:UIControlEventTouchUpInside];
    //        [topV addSubview:cancelBtn];
//    cell.textLabel.text = [NSString stringWithFormat:@"消息 %ld", indexPath.row + 1];
//    cell.textLabel.textColor = [UIColor textGrayColor];
    
    return cell;
}

#pragma mark - UITableViewDelegate
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return ALIGN_HEIGHT_PT(208);
//}



//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    return 0.01;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 10;
//}
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView *headView = [[UIView alloc]init];
//    headView.backgroundColor = [UIColor orangeColor];
//    return headView;
//}

-(ZJMessageNetworking *)manager{
    if (!_manager) {
        _manager = [ZJMessageNetworking new];
    }
    return _manager;
}


- (NSDictionary *)parameters {
    return @{
        @"type": self.type ?: @"",
        @"pageNo": @(self.pageNo),
        @"pageSize": @(self.pageSize)
    };
}
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


-(publicNodataView *)nodataView{
    if (!_nodataView) {
        _nodataView = [publicNodataView new];
        _nodataView.hidden = YES;
    }
    return _nodataView;
}

@end
